<?php

namespace IDock\Insurance\ACIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Printer\ProposalStatusPrinter;

class ProposalStatusResponse extends AllACICResponse
{
    public function printerInit(): Response
    {
        $proposalStatusPrinter = new ProposalStatusPrinter();

        //核保失败
        if (!$this->getBody()) {
            $proposalStatusPrinter->statusCode = 'U4';
            $this->printer = $proposalStatusPrinter;
            $proposalStatusPrinter->status = IDockEnums::$underStats[$proposalStatusPrinter->statusCode] ?? '';
            return $this;
        }

        $biStats = $this->get('BI')['UWStatus'] ?? '1';

        $ciStats = $this->get('CI')['UWStatus'] ?? '1';

        if ($biStats == '3' or $ciStats == '3') {
            $proposalStatusPrinter->statusCode = 'U5';
        }

        //核保通过
        if ($ciStats == '1' and $biStats == '1') {
            $proposalStatusPrinter->statusCode = 'U1';
        }

        //人工核保
        if ($biStats == '6' or $ciStats == '6') {
            $proposalStatusPrinter->statusCode = 'U3';
        }

        //核保退回
        if ($biStats == '4' or $ciStats == '4') {
            if ($UWDesc = $this->get('BI')['UWDesc'] ??'') {
                $proposalStatusPrinter->messageTip = $UWDesc;
            } else {
                if ($UWDesc = $this->get('CI')['UWDesc'] ??'') {
                    $proposalStatusPrinter->messageTip = $UWDesc;
                }
            }

            $proposalStatusPrinter->statusCode = 'U2';
        }

        //待审核
        if ($biStats == '0' and $ciStats == '0') {
            $proposalStatusPrinter->statusCode = 'U7';
        }

        //出单数据优化
        if ($proposalStatusPrinter->statusCode == 'U5') {
            $proposalStatusPrinter->ciInquiryNo = $this->get('CI')['CConfirmNo'] ?? '';
            $proposalStatusPrinter->ciPolicyNo = $this->get('CI')['CPolicyNo'] ?? '';
            $proposalStatusPrinter->ciStartData = $this->get('CI')['ExpireDate'] ?? '';
            $proposalStatusPrinter->ciEndData = $this->get('CI')['EffectiveDate'] ?? '';

            $proposalStatusPrinter->biInquiryNo = $this->get('BI')['CConfirmNo'] ?? '';
            $proposalStatusPrinter->biPolicyNo = $this->get('BI')['CPolicyNo'] ?? '';
            $proposalStatusPrinter->biStartData = $this->get('BI')['ExpireDate'] ?? '';
            $proposalStatusPrinter->biEndData = $this->get('BI')['EffectiveDate'] ?? '';

            $proposalStatusPrinter->underDate = $this->get('BI')['UWTime'] ?? $this->get('CI')['UWTime'] ?? '';
            $proposalStatusPrinter->payDate = $this->get('BI')['PolicyCrtTime'] ?? $this->get('CI')['PolicyCrtTime'] ?? '';

            if ($proposalStatusPrinter->underDate) {
                $proposalStatusPrinter->underDate = date('Y-m-d H:i:s', strtotime($proposalStatusPrinter->underDate));
            }

            if ($proposalStatusPrinter->payDate) {
                $proposalStatusPrinter->payDate = date('Y-m-d H:i:s', strtotime($proposalStatusPrinter->payDate));
            }
        }

        $proposalStatusPrinter->status = IDockEnums::$underStats[$proposalStatusPrinter->statusCode] ?? '';

        $this->printer = $proposalStatusPrinter;

        return $this;
    }
}